//地图内的数据相关处理，点、线
import { getSubwayHouseCount } from "@/common/api";
import { getProdPointsOfLine } from "@/common/api/houses";
import { isIos } from '@/common/common';

export const saleTypeColor = {
    chou: '#0F49BC',//认购中 -->认购中
    wait_yaohao: '#0F49BC',//待摇号  -->认购中
    wait_yaohao: '#0F49BC',//待摇号   -->认购中
    sale: '#D81C03',//在售中  -->在售中
    yaohao: '#D81C03',//已摇号   -->在售中
    end: '#999999',//已售罄  -->已售罄
    has_presale_card: '#5FAADD',//待取证  -->待销售
    wait_sale: '#5FAADD',//待销售  -->待销售
    wait_presale: '#5FAADD',//即将预售  -->待销售
    pre_chou: '#5FAADD',//即将认购   -->待销售
    wait_add: '#5FAADD',//待加推   -->待销售
}

/**
 * @desc 根据地铁线路获取所有站点和线路信息并返回
 * @param cityCode {String} 城市
 * @param subways {Array} 所有线路
 * @param currentLine {Number} 对应的线路下标
 * @return {polylineArr:[], stationArr:[], color: ''}
*/
export async function getPolylineAndStationOfLine({cityCode, subways, currentLine}) {
    let linesAndPointsObj = {
        polylineArr: [],
        stationArr: [],
        color: ''
    };
    let currLineObj = subways[currentLine];
    if(currLineObj.station.length === 0) {
        let stationRes = await getSubwayHouseCount({
            city: cityCode,
            subway: subways[currentLine].value
        })

        if(stationRes.status !== 0) return linesAndPointsObj;

        let { polyline, station, color } = stationRes.detail;
        let newCoors = [];
        for(let i=0;i<polyline.length;i++){
            newCoors.push({
                longitude: Number(polyline[i]),
                latitude: Number(polyline[i+1])
            })
            i++
        }
        currLineObj.polyline = newCoors;
        
        currLineObj.station = [...station];
        currLineObj.color = color;
    }

    linesAndPointsObj.color = currLineObj.color;
    linesAndPointsObj.polylineArr = currLineObj.polyline;
    
    linesAndPointsObj.stationArr = (currLineObj.station || []).map((item, index) => ({
        ...item,
        id: index+1,
        latitude: item.lat,
        longitude: item.lng,
        width: 6,
        heigth: 6,
        height: 6,
        iconPath: 'https://image.srea.org.cn/xcx/icon/circle.png',
        label: {
            content: `${item.title}（${item.count}）`,
            color: '#333333',
            fontSize: 10,
            bgColor: '#FFFFFF',
            borderWidth: 1,
            borderColor: '#CCCCCC',
            borderRadius: 30,
            padding: 3,
            anchorX: 10,
            anchorY: -16
        }
    }))

    return linesAndPointsObj;
}

/**
 * 根据坐标点格式化marker并返回
 * @param cityCode {String}
 * @param lineObj {Object} 改线路的对象，label、value、products、color
 * 
*/
export async function getLineNearbyMarkers({cityCode, lineObj}) {
    if(lineObj.products.length > 0) return lineObj.products;

    let markers = [];

    let pointsRes = await getProdPointsOfLine({
        cityCode: cityCode,
        line: lineObj.value
    });

    if(pointsRes.status !== 0) return markers;

    if(pointsRes.length === 0) return markers;
    
    markers = pointsRes.detail.map(item => ({
        ...item,
        type: 'product',
        latitude: item.lat,
        longitude: item.lng,
        width: 6,
        heigth: 6,
        height: 6,
        alpha: isIos() ? 0 : 1,
        callout: {
            content: item.title,
            padding: 4,
            borderRadius:10,
            color: '#ffffff',
            fontSize: 10,
            display: 'ALWAYS',
            textAlign: 'center',
            bgColor: saleTypeColor[item.sell_type] 
        }
    }))
    return markers;
}

/**
 * 根据坐标点判断是否在多边形内
 * @param point {Array} 坐标点
 * @param polygon {Array} 多边形
 * @return {Boolean} 是否在多边形内
*/
export function isPointInPolygon(point, polygon) {
    if(!point || point.length<2) {
        return false
    }
    if(!polygon || polygon.length<4) {
        return false
    }
    
    var nCross = 0;
    var n = polygon.length;
    for(let i=0; i<n; i++) {
        var p1 = polygon[i];
        var p2 = polygon[(i+1)%n];
        if(Number(p1.longitude) == Number(p2.longitude)) continue;
        
        if(Number(point[0]) < Math.min(Number(p1.longitude), Number(p2.longitude))) continue;
        
        if(Number(point[0]) >= Math.max(Number(p1.longitude), Number(p2.longitude))) continue;
        
        var y = (Number(point[0]) - Number(p1.longitude)) * (Number(p2.latitude) - Number(p1.latitude)) / (Number(p2.longitude) - Number(p1.longitude)) + (Number(p1.latitude))
        
        if(y > Number(point[1])) nCross++;
    }
    return (nCross%2 === 1)
}